package com.pur.zsms.interceptor;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.pmsnmanage.menu.po.MenuPo;
import com.pur.zsms.pmsnmanage.menu.service.IMenuService;
import com.pur.zsms.pmsnmanage.role.po.RoleMenuPo;
import com.pur.zsms.pmsnmanage.role.service.IRoleService;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.service.IUserService;

/**
 * 
 * @author zhouz
 * 
 */
public class CheckPermissionIntercepter implements HandlerInterceptor {
	@Resource(name = "roleService")
	private IRoleService roleService;

	@Resource(name = "userService")
	private IUserService userService;
	@Resource(name = "menuService")
	private IMenuService menuService;

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	/**
	 * 请求处理前调用
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		// System.out.println("进入拦截器");
		// System.out.println(request.getServletPath());
		// System.out.println(request.getContextPath());
		String beforPath = request.getServletPath();
		int start = beforPath.indexOf("/");
		int end = beforPath.lastIndexOf("/");
		String path = beforPath.substring(start, end + 1);
		// System.out.println("path@@@@@"+path);
		if (request.getServletPath().startsWith("/user/login.do") || request.getServletPath().equals("/jsp/login.jsp")
				|| request.getServletPath().startsWith("/jsp/nosession.jsp") || request.getServletPath().startsWith("/jsp/logout.jsp")) {
			return true;
		}
		// 如果左等于/images访问, 直接通过
		if (request.getServletPath().startsWith("/images") || request.getServletPath().startsWith("/static")) {
			return true;
		}
		if (path.equals("/querycode/")) {
			// System.out.println("querycodeOK");
			return true;
		}
		if (beforPath.equals("/user/menu.do")) {
			// System.out.println("/user/menu.doOK");
			return true;
		}
		if (beforPath.equals("/user/weixin.do")) {
			// System.out.println("/user/menu.doOK");
			return true;
		}

		// 微信
		if (beforPath.equals("/weixintest/weixintest.do") || beforPath.equals("/weixintest/weixinOrder.do")
				|| request.getServletPath().startsWith("/jsp-mobile/customOrderForSup_mobile.jsp")
				|| beforPath.equals("/weixintest/associationOpenId.do") || beforPath.equals("/jsp/weixinJumpOrderForSupplier1.jsp")
				|| beforPath.equals("/jsp/weixinJumpOrderForSupplier2.jsp") || beforPath.equals("/jsp/weixinJumpOrderForSupplier3.jsp")
				|| beforPath.equals("/weixintest/weixinJumploginOut.do") || beforPath.equals("/weixintest/weixinOrderForSupToCust.do")) {
			// System.out.println("/user/menu.doOK");
			return true;
		}

		if (beforPath.equals("/jsp/weixinlogin.jsp") || beforPath.equals("/jsp/weixinMenuForOut.jsp")) {
			// System.out.println("/user/menu.doOK");
			return true;
		}

		if (beforPath.equals("/jsp/weixinJump.jsp") || beforPath.equals("/jsp/weixin/message/NoHaveUser.jsp")
				|| beforPath.equals("/jsp/weixin/message/successWeixin.jsp")) {
			// System.out.println("/user/menu.doOK");
			return true;
		}

		// 如果用户尚未登录, 转向登录页面
		UserPo user = (UserPo) request.getSession().getAttribute("user");
		if (user == null) {
			response.sendRedirect(request.getContextPath() + "/jsp/login.jsp");
		}
		// System.out.println("user"+user.getUserId()+"@"+user.getUserName());
		// 获取用户角色
		List<RoleVo> roles = userService.listRoleByUserId(Long.toString(user.getUserId()));
		// 用户所有菜单
		List<MenuPo> menus = new ArrayList<MenuPo>();
		for (int i = 0; i < roles.size(); i++) {
			if (roles.get(i).getChecked().equals("true")) {
				List<RoleMenuPo> rolemenus = roleService.findMenusByRole(roles.get(i).getRoleId());
				for (int j = 0; j < rolemenus.size(); j++) {
					menus.add(menuService.findMenu(Long.toString(rolemenus.get(j).getMenuId())));
				}
			}
		}

		// System.out.println("menus.size@"+menus.size());
		for (int i = 0; i < menus.size(); i++) {

			if (path.equals(menus.get(i).getMenuPath())) {
				// System.out.println("*******"+menus.get(i).getMenuPath());
				// System.out.println("OK@"+path);
				return true;
			}
		}

		// 暂时关闭
		return true;
	}

}
